﻿#region 命名空间

using System;
using System.Web.UI.WebControls;
using WinnerCMS.BLL;
using WinnerCMS.Common;
using WinnerCMS.Model;

#endregion

namespace WinnerCMS.WebUI.Templates.WinnerCMS6.Member
{
    public partial class Message : WebFactory.ClientPublicPage
    {
        private string Action;

        protected void Page_Load(object sender, EventArgs e)
        {
            Page.Title = WebConfig.SiteName + "→ 会员中心 → 短信管理";
            this.Action = Request["Action"];
            if (RightsCheck.IsLogined == false)
            {
                MsgBox.Message("登录超时，请重新登录。", "Login.aspx", this);
            }
            if (!IsPostBack)
            {
                Bind(1);
            }
        }

        private void Bind(int PageNo)
        {
            var Action = Request["Action"];
            var pp = new PagingParam();
            pp.PageSize = this.PageNavigation1.PageSize;
            pp.PageNo = PageNo;
            if (string.IsNullOrEmpty(Action) || Action == "Inbox") //收件箱
            {
                this.lblPath.Text = "收件箱";

                this.GV_List.DataSource = new MessageManage().BindList("Incept", UserManage.Current.UserName, pp);
                this.GV_List.DataBind();
                this.PageNavigation1.RecordCount = pp.RecordCount;
                if (this.GV_List.Rows.Count <= 0)
                {
                    this.lblNoShow.Text = "<img src='/Images/NoData.gif'><br/>";
                }
                else
                {
                    this.lblNoShow.Visible = false;
                }
            }
            else if (Action == "Outbox") //发件箱
            {
                this.lblPath.Text = "发件箱";

                this.GV_List.DataSource = new MessageManage().BindList("Sender", UserManage.Current.UserName, pp);
                this.GV_List.DataBind();
                this.PageNavigation1.RecordCount = pp.RecordCount;
                if (this.GV_List.Rows.Count <= 0)
                {
                    this.lblNoShow.Text = "<img src='/Images/NoData.gif'><br/>";
                }
                else
                {
                    this.lblNoShow.Visible = false;
                }
            }
            else if (Action == "Trash") //废件箱
            {
                this.lblPath.Text = "废件箱";

                this.GV_List.DataSource = new MessageManage().BindList("DelSend", UserManage.Current.UserName, pp);
                this.GV_List.DataBind();
                this.PageNavigation1.RecordCount = pp.RecordCount;
                if (this.GV_List.Rows.Count <= 0)
                {
                    this.lblNoShow.Text = "<img src='/Images/NoData.gif'><br/>";
                }
                else
                {
                    this.lblNoShow.Visible = false;
                }
            }
        }

        protected void GV_List_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                var myTableCell = e.Row.Cells[4]; //回复
                var hl = myTableCell.Controls[0] as HyperLink;
                if (this.Action == "Outbox") //发件箱
                {
                    hl.Enabled = false;
                }

                myTableCell = e.Row.Cells[5]; //删除

                var btn = myTableCell.Controls[0] as LinkButton;
                if (this.Action == "Outbox") //发件箱
                {
                    btn.Enabled = false;
                }
                else
                {
                    btn.Attributes.Add("onclick", "return confirm('您确定要删除？');");
                }
            }
        }

        protected void PageNavigation1_PageChanging(object src, WebControls.PageChangingEventArgs e)
        {
            Bind(e.NewPageIndex);
        }

        protected void GV_List_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            var ID = this.GV_List.DataKeys[e.RowIndex].Value.ToString();

            if (this.Action == "Trash")
            {
                new MessageManage().Delete(ID, this.UpdatePanel1);
            }
            else
            {
                new MessageManage().LogicDeleteOrRestoration(ID, this.UpdatePanel1);
            }

            Bind(1);
        }
    }
}